package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.dbscanhistory.DbScanHistoryPo;
import com.bwda.dsrs.filecenter.domain.condition.dbscanhistory.DbScanHistoryCondition;
import com.bwda.dsrs.filecenter.domain.form.dbscanhistory.DbScanHistoryQueryForm;
import com.bwda.dsrs.filecenter.domain.form.dbscanhistory.DbScanHistoryUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.dbscanhistory.DbScanHistoryCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.dbscanhistory.DbScanHistoryVo;
import com.bwda.dsrs.filecenter.service.DbScanHistoryService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/dbscanhistory")
@Api(value = "/dbscanhistory", tags = "敏感字段扫描结果历史表")
public class DbScanHistoryController extends BaseController {

	@Autowired
	private DbScanHistoryService dbScanHistoryService;

	@ApiOperation(value = "查询敏感字段扫描结果历史表",notes = "查询敏感字段扫描结果历史表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DbScanHistoryVo> query(@ApiParam(value = "id", required = true)@RequestParam Integer scanHistoryId) throws BusinessServiceException {
		DbScanHistoryPo po = dbScanHistoryService.queryWithValid(scanHistoryId);
		DbScanHistoryVo vo = CopyUtil.transfer(po, DbScanHistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询敏感字段扫描结果历史表数量",notes = "查询敏感字段扫描结果历史表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid DbScanHistoryQueryForm form) throws BusinessServiceException {
		DbScanHistoryCondition condition = this.getConditionByQueryForm(form);
		int count = dbScanHistoryService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询敏感字段扫描结果历史表列表",notes = "查询敏感字段扫描结果历史表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DbScanHistoryVo>> queryList(@ModelAttribute@Valid DbScanHistoryQueryForm form) throws BusinessServiceException {
		DbScanHistoryCondition condition = this.getConditionByQueryForm(form);
		List<DbScanHistoryPo> poList = dbScanHistoryService.queryList(condition);
		List<DbScanHistoryVo> voList = CopyUtil.transfer(poList, DbScanHistoryVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询敏感字段扫描结果历史表列表(带分页)",notes = "查询敏感字段扫描结果历史表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DbScanHistoryVo>> queryPageList(@ModelAttribute@Valid DbScanHistoryQueryForm form) throws BusinessServiceException {
		DbScanHistoryCondition condition = this.getConditionByQueryForm(form);
		List<DbScanHistoryVo> voList = new ArrayList<>();
		int count = dbScanHistoryService.queryCount(condition);
		if (count > 0) {
			List<DbScanHistoryPo> poList = dbScanHistoryService.queryList(condition);
			voList = CopyUtil.transfer(poList, DbScanHistoryVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增敏感字段扫描结果历史表",notes = "新增敏感字段扫描结果历史表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DbScanHistoryVo> add(@ModelAttribute@Valid DbScanHistoryCreateForm form) throws BusinessServiceException {
		DbScanHistoryPo po = CopyUtil.transfer(form, DbScanHistoryPo.class);
//		po.setScanHistoryId(RandomUtils.nextInt());
		dbScanHistoryService.insert(po);
		DbScanHistoryVo vo = CopyUtil.transfer(po, DbScanHistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改敏感字段扫描结果历史表",notes = "修改敏感字段扫描结果历史表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid DbScanHistoryUpdateForm form) throws BusinessServiceException {
		DbScanHistoryPo po = CopyUtil.transfer(form, DbScanHistoryPo.class);
		dbScanHistoryService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除敏感字段扫描结果历史表",notes = "删除敏感字段扫描结果历史表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "id", required = true)@RequestParam Integer scanHistoryId) throws BusinessServiceException {
		dbScanHistoryService.delete(scanHistoryId);
		return getSuccessResult();
	}

	/**
	 * DbScanHistoryQueryForm转换为DbScanHistoryCondition
	 *
	 * @param form
	 * @return
	 */
	private DbScanHistoryCondition getConditionByQueryForm(DbScanHistoryQueryForm form) {
		DbScanHistoryCondition condition = CopyUtil.transfer(form, DbScanHistoryCondition.class);
		return condition;
	}

}